/**
 * 
 */
package org.dg.iag.server;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.dg.iag.dao.GenericDAO;
import org.dg.iag.entity.Settings;
import org.dg.iag.entity.SettingsInstance;
import org.dg.iag.interf.entity.GenericRestResource;
import org.dg.iag.query.XQueryGet;
import org.dg.iag.util.ApplicationConstants;
import org.dg.iag.util.LoginUtil;
import org.dg.iag.util.scheduler.SchedulerManager;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

/**
 * @author Alex Gartner
 *
 */
public class SettingsInstanceServerResource extends ServerResource implements
		GenericRestResource<SettingsInstance> {

	private static Logger logger	= Logger.getLogger(SettingsInstanceServerResource.class);
	/* (non-Javadoc)
	 * @see org.dg.iag.interf.entity.GenericRestResource#getElement()
	 */// TODO: handle exception
	@Override
	public SettingsInstance getElement() {
		if ( ! LoginUtil.checkLogin(getRequest()) ) 
			return null;
		String id= (String) this.getRequestAttributes().get("id");
		GenericDAO<SettingsInstance, Settings> gdao 	= 
				new GenericDAO<SettingsInstance, Settings>(ApplicationConstants.SETTINGS_XML);
		SettingsInstance si = gdao.getElement( SIResource.class, "/Settings/SettingsInstance[./id='"+id+"']");
		return si;
	}

	/* (non-Javadoc)
	 * @see org.dg.iag.interf.entity.GenericRestResource#addElement(java.lang.Object)
	 */
	@Post
	@Override
	public void addElement(SettingsInstance element) {
		if ( ! LoginUtil.checkLogin(getRequest()) ) 
			return ;
		GenericDAO<SettingsInstance, Settings> gdao	= new GenericDAO<SettingsInstance, Settings>(ApplicationConstants.SETTINGS_XML);
		
		XQueryGet xqg			= new XQueryGet("", "max(/Settings/SettingsInstance/id)", false);
		
		String result			= gdao.getElementsAsString(xqg);
		try {
			long maxId			= Long.parseLong(result);
			element.setId(maxId+1);
			gdao.addElement(element, "/Settings");
			
			SchedulerManager sm		= SchedulerManager.getInstance();
			if (sm != null) {
				sm.reboot();
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage() );
		}
		
	}

	/* (non-Javadoc)
	 * @see org.dg.iag.interf.entity.GenericRestResource#modifyElement(java.lang.Object)
	 */
	@Put
	@Override
	public Object modifyElement(SettingsInstance element) {
		if ( ! LoginUtil.checkLogin(getRequest()) ) 
			return null;
		GenericDAO<SettingsInstance, Settings> gdao	= new GenericDAO<SettingsInstance, Settings>(ApplicationConstants.SETTINGS_XML);
		gdao.modifyElement(element, "/Settings/SettingsInstance[./id='" + element.getId() + "']");
		
		SchedulerManager sm		= SchedulerManager.getInstance();
		if (sm != null) {
			sm.reboot();
		}
		else
			logger.warn("Schedule Manager is null although it's not supposed to be");
		
		Settings settings	= new Settings();
		settings.setSettingsInstances(new ArrayList<SettingsInstance>(1));
		settings.getSettingsInstances().add(element);
		return settings;
	}

	public interface SIResource extends GenericRestResource<SettingsInstance> {}
}
